package com.wjt.homestayms.controller;

import com.wjt.homestayms.common.Result;
import com.wjt.homestayms.pojo.Employee;
import com.wjt.homestayms.common.PageRequest;
import com.wjt.homestayms.common.PageResult;
import com.wjt.homestayms.pojo.Orderinfo;
import com.wjt.homestayms.service.IEmpService;
import com.wjt.homestayms.service.IOrderinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
public class EmpController {

    @Autowired
    IEmpService empService;
    @Autowired
    IOrderinfoService orderinfoService;
    /**
     * 根据id获取员工列表
     *
     * @param pageRequest（pageNum，pageSize）
     * @param id
     * @return
     */
    @GetMapping("/getEmpList")
    @ResponseBody
    public Map<String, Object> getEmpList(PageRequest pageRequest, Integer id,String empName) {
        Map<String, Object> map = new HashMap<>();
        PageResult resultPage = null;
        if (id != null) {
            resultPage = empService.getEmpList(pageRequest, id);
        }else if(empName != null){
            resultPage = empService.getEmp(pageRequest, empName);
        }
        return Result.queryResult(map,resultPage);
    }

    /**
     * 根据eId获取一个员工信息。
     *
     * @param eId
     * @return
     */
    @GetMapping("/getEmpByEid")
    @ResponseBody
    public Result<Employee> getEmpByEid(Integer eId) {
        Map<String, Object> map = new HashMap<>();
        Employee emp = empService.getEmp(eId);
        if (emp != null) {
            return Result.ok(emp);
        }

        return Result.fail("对象为空");
    }

    /**
     * 添加員工信息，参数是括号中写的那些。
     *
     * @param employee （empName,phoneNum,salary,title,id）
     * @return
     */
    @PostMapping("addEmp")
    @ResponseBody
    public Result addEmp(Employee employee) {
        if (empService.addEmp(employee)) {
            return Result.ok(true);
        } else {
            return Result.fail("添加失败");
        }
    }

    /**
     * 更新员工信息
     *
     * @param employee（eId,empName,phoneNum,salary,title）
     * @return
     */
    @PostMapping("updateEmp")
    @ResponseBody
    public Result updateEmp(Employee employee) {
        Map<String, Object> map = new HashMap<>();
        if (empService.updateEmp(employee)) {
            return Result.ok(true);
        } else {
            return Result.fail("更新失败");
        }
    }

    /**
     * 根据eId，删除员工信息。
     *
     * @param eId
     * @return
     */
    @GetMapping("deleteEmp")
    @ResponseBody
    public Result deleteEmp(Integer eId) {
        Map<String, Object> map = new HashMap<>();
        if (empService.deleteEmp(eId)) {
            return Result.ok(true);
        } else {
            return Result.fail("删除");
        }
    }

    @GetMapping("countSalary")
    @ResponseBody
    public Result countSalary(Integer id){
        List<Employee> emps = empService.getEmps(id);
        List<Orderinfo> orderinfos = orderinfoService.getOrderinfos(id);
        Iterator<Employee> iterator = emps.iterator();
        Iterator<Orderinfo> iterator1 = orderinfos.iterator();
        Integer outCount = 0;
        Integer inCount = 0;
        while (iterator.hasNext()){
            Employee employee = iterator.next();
            outCount = outCount + employee.getSalary();
        }

        while (iterator1.hasNext()){
            Orderinfo orderinfo = iterator1.next();
            inCount = inCount + orderinfo.getPrice();
        }
        Map<String,Integer> map = new HashMap<>();
        map.put("outCount",outCount);
        map.put("inCount",inCount);
        return Result.ok(map);
    }

}
